package com.esp.commentdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.esp.commentdemo.dao.NewsCommentMapper;
import com.esp.commentdemo.entity.NewsComment;
import com.esp.commentdemo.service.CommentService;
import com.esp.commentdemo.util.PageQueryUtil;
import com.esp.commentdemo.util.PageResult;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Project:spring-boot-new-publish-system-12
 * ClassName:CommentServiceImpl
 * Package:com.esp.commentdemo.service.impl
 * User:CWQ
 * Description:
 *
 * @Author 合肥-陈万琼
 * @Create 2024/5/31 23:38
 * @Version 1.0
 */
@Service
public class CommentServiceImpl extends ServiceImpl<NewsCommentMapper, NewsComment> implements CommentService {
    @Resource
    private NewsCommentMapper newsCommentMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public Boolean addComment(NewsComment newsComment) {
        return newsCommentMapper.insert(newsComment) > 0;
    }

    @Override
    public PageResult getCommentsPage(PageQueryUtil pageUtil) {
        int page = pageUtil.getPage();
        int limit = pageUtil.getLimit();

        QueryWrapper<NewsComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0)
                .orderByDesc("news_id");

        Page<NewsComment> newsCommentPage = new Page<>(page,limit);

        Page<NewsComment> comments = newsCommentMapper.selectPage(newsCommentPage, queryWrapper);

        int total = Math.toIntExact(newsCommentMapper.selectCount(queryWrapper));
        PageResult pageResult = new PageResult(comments.getRecords(), total, limit, page);
//        List<NewsComment> comments = newsCommentMapper.findNewsCommentList(pageUtil);
//        int total = newsCommentMapper.getTotalNewsComments(pageUtil);
//        PageResult pageResult = new PageResult(comments, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public Boolean checkDone(Integer[] ids) {
        UpdateWrapper<NewsComment> wrapper = new UpdateWrapper<>();
        wrapper.set("comment_status", 1)
                .in("comment_id", ids)
                .eq("comment_status", 0)
                .in("comment_id", Arrays.asList(ids));
        return newsCommentMapper.update(wrapper) > 0;
    }

    @Override
    public Boolean deleteBatch(Integer[] ids) {
        List<Integer> listId = Arrays.asList(ids);
        return newsCommentMapper.deleteBatchIds(listId)> 0;
    }


    @Override
    public Boolean thumbsUp(Long newsId) {
        QueryWrapper<NewsComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("news_id", newsId);
        NewsComment newsComment = newsCommentMapper.selectOne(queryWrapper);
        if(newsComment == null ){
            return false;
        }
        String thumbsKey =  "thumbsupScore";
        redisTemplate.opsForZSet().incrementScore(thumbsKey, "thumbsup" + newsId, 1);
        return true;
    }

    @Override
    public Boolean thumbsDown(Long newsId) {
        QueryWrapper<NewsComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("news_id", newsId);
        NewsComment newsComment = newsCommentMapper.selectOne(queryWrapper);
        if(newsComment == null ){
            return false;
        }
        String thumbsKey =  "thumbsupScore";
        redisTemplate.opsForZSet().incrementScore(thumbsKey, "thumbsup" + newsId, -1);
        return true;
    }

    @Override
    public int getThumbsdNum(Long newsId) {
        String thumbsKey =  "thumbsupScore";
        Double score = redisTemplate.opsForZSet().score(thumbsKey, "thumbsup2");
        return score.intValue();
    }
}
